package com.trkj.service.cf;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.print.attribute.standard.PageRanges;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.cf.OrganizationStructureDAO;
import com.trkj.pojo.OrganizationArea;
import com.trkj.pojo.OrganizationStructure;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor=Exception.class)
public class OrganizationStructureService {
		@Autowired
		private OrganizationStructureDAO organizationStructureDAO;
			
		/**
		 *  查询组织结构版本     所有
		 */
		public List<OrganizationStructure> OrganizationStructureall(){
			List<OrganizationStructure> list= (List<OrganizationStructure>) organizationStructureDAO.findAll();
			return list;
		}
		//搜索
		public List<OrganizationStructure> filedByName(String versionName)throws ServiceException{
			try {
				return organizationStructureDAO.queryByUserName("%"+versionName+"%");
			} catch (Exception e) {
				e.printStackTrace();
			   throw new ServiceException(e.getMessage());
			}
		}
			//获取总记录条数
		public long pageCount() {
			return organizationStructureDAO.count();
		}
		        
			//分页
		public List<OrganizationStructure> userPage(Integer page, Integer rows)throws ServiceException {
			try {
			List<OrganizationStructure> list= organizationStructureDAO.queryByPage(new PageRequest(page-1,rows));
			return  list;
			} catch (Exception e) {
				e.printStackTrace();
			   throw new ServiceException(e.getMessage());
			}
		}
		//新增        
		public void OrganizationStructureadds(Long organsId, String versionName, Timestamp  organsStartdate, Timestamp organsEnddate, String organsRemark,
				Integer organsState) throws ServiceException {
			System.out.println(organsId+" "+versionName +" "+organsStartdate +" "+organsEnddate +" "+organsRemark +" "+organsState);
			try {
				Date date=new Date();
				OrganizationStructure o=new OrganizationStructure();
				o.setOrgansId(organsId);
				o.setVersionName(versionName);
				o.setOrgansStartdate(organsStartdate==null?new Timestamp(date.getMonth()+date.getDay()):organsStartdate);
				o.setOrgansEnddate(organsEnddate==null?new Timestamp(date.getMonth()+date.getDay()):organsStartdate);
				o.setOrgansRemark(organsRemark);
				o.setOrgansState(organsState);
				organizationStructureDAO.save(o);
				} catch (Exception e) {
					e.printStackTrace();
				   throw new ServiceException(e.getMessage());
				}
		}
		
				//修改       
				public void organizationupdate(Long organsId, String versionName, String organsRemark,
						Integer organsState) throws ServiceException {
					try {
						Date date=new Date();
						Timestamp ddd=new Timestamp(date.getTime());
						DateFormat dateforrmat=new SimpleDateFormat();
						
						OrganizationStructure  organiza=organizationStructureDAO.findOne(organsId);
						organiza.setVersionName(versionName);
						organiza.setOrgansStartdate(ddd);
						organiza.setOrgansEnddate(ddd);
						organiza.setOrgansRemark(organsRemark);
						organiza.setOrgansState(organsState);
						organizationStructureDAO.save(organiza);
						} catch (Exception e) {
							e.printStackTrace();
						   throw new ServiceException(e.getMessage());
						}
				}

				//删除 
				public void organizationremove(Long organsId) throws ServiceException {
					try {
						organizationStructureDAO.delete(organsId);
						} catch (Exception e) {
							e.printStackTrace();
						   throw new ServiceException(e.getMessage());
						}
				}
			 
}
